package com.zzs.team.Service;

import com.zzs.team.DAO.LikeDao;
import com.zzs.team.DAO.ReservationDao;
import com.zzs.team.DAO.TeamDao;
import com.zzs.team.Entity.Location;
import com.zzs.team.Entity.Team;
import com.zzs.team.Entity.TeamShow;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TeamServiceImpl implements TeamService {

    @Autowired
    TeamDao teamDao;

    @Autowired
    LikeDao likeDao;

    @Autowired
    ReservationDao reservationDao;

    @Override
    public TeamShow getTeamById(String userID, Integer teamid) {
        List<TeamShow> teams = teamDao.getTeamById(teamid);
        return dealLikesAndReservations(userID,teams).get(0);
    }

    @Override
    public void insertTeam(Team team) {
        //1、设置创建时间
        team.setCreateTime(new Date());
        teamDao.InsertTeam(team);
    }

    @Override
    public List<TeamShow> getTeams(String userid) {
        List<TeamShow> teams = teamDao.getTeams();

        return dealLikesAndReservations(userid,teams);
    }

    @Override
    public List<TeamShow> getTeamsWithoutUserID() {
        List<TeamShow> teamShows = teamDao.getTeams();
        for (TeamShow teamShow :teamShows){
            /*将发布人的openid全部去掉*/
            teamShow.setUserID(null);
            teamShow.getUser().setOpenID(null);
        }
        return teamShows;
    }

    @Override
    public List<TeamShow> getTeamsByProvinceName(String userID, String provinceName) {
        List<TeamShow> teams = teamDao.getTeamsByProvinceName(provinceName);
        return dealLikesAndReservations(userID,teams);
    }

    @Override
    public List<TeamShow> getTeamsByCityName(String userID, String cityName) {
        List<TeamShow> teams = teamDao.getTeamsByCityName(cityName);
        return dealLikesAndReservations(userID,teams);
    }

    @Override
    public List<TeamShow> getTeamsBySchoolName(String userID, String SchoolName) {
        List<TeamShow> teams  = teamDao.getTeamBySchoolName(SchoolName);
        return dealLikesAndReservations(userID,teams);
    }

    /**
     * 传入用户的id和要显示的队伍信息，判断用户是否收藏队伍信息,处理完信息的like属性后将信息team返回
     * @param userID
     * @param teams
     * @return
     */
    public List<TeamShow> dealLikesAndReservations(String userID,List<TeamShow> teams){
        //找到当前用户的
        List<Integer> userLikes = likeDao.getUserLikes(userID);
        List<Integer> userReservations = reservationDao.getUserReservations(userID);
        for(TeamShow teamShow:teams){
            for(int like:userLikes){
                if (teamShow.getTeamID()==like){
                    teamShow.setLike(true);
                    break;
                }
            }
            for(int reservation:userReservations){
                if (teamShow.getTeamID() == reservation){
                    teamShow.setReserve(true);
                    break;
                }
            }

            /*查询所所有的发布信息并将发布人的openid全部去掉*/
            teamShow.setUserID(null);
            teamShow.getUser().setOpenID(null);

        }
        return teams;
    }

    @Override
    public List<TeamShow> getMyTeams(String userID) {
        List<TeamShow> myteams = teamDao.getMyTeams(userID);
        //判断用户有没有收藏自己的组队信息，如果有将like设为true
        return dealLikesAndReservations(userID,myteams);
    }

    @Override
    public void deleteTeamByTeamid(String userID, String teamID) {
        teamDao.deleteTeamByTeamid(userID, teamID);
    }

    @Override
    public void updateTeam(String uID, Team t) {
        teamDao.updateTeam(uID, t);
    }


}
